package com.kgc.mj.springbooot.config;

import com.kgc.mj.springbooot.constant.SystemConstant;
import com.kgc.mj.springbooot.service.AsyncChargeServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * Created by MJ on 2020/10/21 9:45
 *
 * 异步线程池配置类
 *
 */
@Configuration
@EnableAsync
public class AsyncExecutorConfig {
    private static final Logger logger = LoggerFactory.getLogger(AsyncChargeServiceImpl.class);

    //读取配置参数
    @Value("${async.thread.core_pool_size}")
    private int corePoolSize;

    //设置最大线程数
    @Value("${async.thread.max_pool_size}")
    private int maxPoolSize;

    //设置队列容量
    @Value("${async.thread.queue_capacity}")
    private int queueCapacity;

    @Bean
    public Executor asyncServiceExecutor(){
        logger.info("==========asyncServiceExcetor 开始==========");
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setThreadNamePrefix(SystemConstant.ASYNC_THREAD_NAME_PREFIX);
        executor.initialize();
        return executor;
    }
}
